﻿/// <reference path="jquery/jquery-1.9.1.js" />
//给window对象注册事件
window.onhashchange = function () {
    if (window.location.hash != '') {
        loadIframe(window.location.hash.replace('#', ''));
    } else {
        loadIframe('welcome');
    }
};
window.onload = function () {
    var iframeSrc = location.href.split('#')[1];
    var text=$.trim($('#iframe').text());
    if (iframeSrc) {
        //有锚点值
        loadIframe(iframeSrc);
    } else if (!text) {
        loadIframe('welcome');
    }

    //后台主页面导航条
    $('.w-nav h3.m-icon').click(function () {
        $(this).parent().toggleClass('current').siblings().removeClass('current');
        $(this).parent().siblings().children('ul').slideUp(200);
        $(this).next('ul').removeClass('hover').slideToggle(200);
    });

};

//改变导航展开收起情况
function changeNav() {
    var h = window.location.hash.replace('#', '');
    h = h.toLowerCase().indexOf('editor')!=-1 ? 'posts' : h;
    $('li.item ul').hide();
    $('li').removeClass('current');
    var $a = $('#navigation .w-nav').find('a[href*="' + h + '"]');
    $a.parent().addClass('current').parent().show().parent().addClass('current');
}

//在内容区加载页面
function loadIframe(fileName) {
    changeNav();
    var pTitle = $('.w-nav .current h3').text();
    var title = $('.w-nav .current li.current a').text();
    document.title = title + ' - ' + pTitle + ' ‹ ' + document.title.split(' ‹ ')[1];
        //(current == '首页' || current == '' ? '仪表盘' : current) + ' ‹ ' + document.title.split(' ‹ ')[1];
    $('.load-tips').fadeIn(200);
    var url = fileName.replace(/(.+)\?(.+)/, '$1?$2');
    //加上参数防止缓存
    url = url != fileName ? url : fileName + '?use=include&timer=' + new Date().getMilliseconds();
    url = url.replace(/(.*?\?.*?)\?(.*)/, '$1&$2');
    
    //$.get("/admin/" + url, {}, function (html) {
    //    $('#iframe').empty().html(html).fadeIn(600);
    //    //全选和全不选
    //    $('.cb-all').click(changeCheck);
    //    $('.load-tips').fadeOut(200);
    //});
    $('#iframe').load("/admin/" + url, {}, function () {
        $('#iframe').fadeIn(600);
        //全选和全不选
        $('.cb-all').click(changeCheck);
        $('.load-tips').fadeOut(200);
    });
}

//全选
function changeCheck() {
    if (this.checked) {
        $('.cb-item').each(function () { this.checked = true; });
        $('.cb-all').each(function () { this.checked = true; });
    } else {
        $('.cb-item').each(function () { this.checked = false; });
        $('.cb-all').each(function () { this.checked = false; });
    }
}

//跳转到登录页
function goToLogin() {
    window.location.href = '/Login?redirect='+encodeURIComponent(window.location.href);
}

var timeOut;
//提示消息函数
function showMsg(message, msgLevel) {
    //启用发布按钮
    $('.btn[data-loading-text]').button('reset');
    $('.post-tip').fadeOut();
    //提示消息
    $('#message').attr('class', 'alert ' + msgLevel);
    $('#message span').empty().append(message);
    $('#message').fadeIn(300);
    timeOut = setTimeout(function () {
        $('#message').removeAttr('class').fadeOut(500);
    }, 20000);
}